package com.sqk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqk.reggie.dto.DishDto;
import com.sqk.reggie.mapper.DishMapper;
import com.sqk.reggie.pojo.Category;
import com.sqk.reggie.pojo.Dish;
import com.sqk.reggie.pojo.DishFlavor;
import com.sqk.reggie.service.CategoryService;
import com.sqk.reggie.service.DishFlavorService;
import com.sqk.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息
        this.save(dishDto);

        Long dishId = dishDto.getId(); //菜品id

        List<DishFlavor> flavors = dishDto.getFlavors(); //将菜品dish的id，写入菜品口味表dish_flavor中
        flavors = flavors.stream().map((item) ->{
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 修改菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto){
        //修改保存菜品的基本信息
        this.updateById(dishDto);

        Long dishId = dishDto.getId(); //获取菜品id

        //修改前删除口味
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishId);
        dishFlavorService.remove(queryWrapper);

        //修改前保存菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors(); //将菜品dish的id，写入菜品口味表dish_flavor中
        flavors = flavors.stream().map((item) ->{
            item.setId(null);
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

  /**
     * 删除菜品同时删除对应的口味
     * @param ids
     */
    @Override
    @Transactional
    public void removeDishWithFlavor(String ids) {
        //拆分ids
        String[] idsA = ids.split(",");

        //删除菜品对应的口味
        LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper = new LambdaQueryWrapper<>();
        Arrays.stream(idsA).forEach(item -> {
            long id = Long.parseLong(item);

            //根据dishId获取菜品
            Dish newDish = dishService.getDishById(Long.valueOf(item));
            String key = "dish_" + newDish.getCategoryId() + "_1";
            //删除redis中的数据
            redisTemplate.delete(key);

            //删除菜品
            dishService.removeById(id);
            //删除菜品对应的口味
            dishFlavorQueryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(dishFlavorQueryWrapper);
        });
    }


    /**
     * 回显数据
     * @param id
     * @return
     */
    public DishDto getDishById(Long id) {
        //创建DishDto
        DishDto dishDto = new DishDto();

        //根据id查询Dish数据
        Dish dish = this.getById(id);

        //获取菜品口味数据
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //根据Dish数据的id查询dish_flavor表
        queryWrapper.eq(DishFlavor::getDishId, id);
        //获取菜品口味
        List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper);

        //将查出的Dish数据封装为DishDto
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }
}













